package com.party.service.exam.impl;

import com.party.common.Page;
import com.party.mapper.ExamAnswerMapper;
import com.party.mapper.ExamMapper;
import com.party.mapper.ExamTestMapper;
import com.party.model.ExamAnswer;
import com.party.model.ExamTest;
import com.party.model.ExamTestExample;
import com.party.service.exam.ExamAnswerService;
import com.party.service.exam.ExamTestService;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: beyond
 * Date: 14-10-9
 * Time: 下午8:04
 * To change this template use File | Settings | File Templates.
 */
@Repository
public class ExamTestServiceImpl implements ExamTestService {

    @Resource
    ExamTestMapper examTestMapper;

    @Resource
    ExamAnswerMapper examAnswerMapper;

    @Resource
    ExamAnswerService examAnswerService;

    @Override
    public int addExamTestAndExamAnswer(ExamTest examTest,List<ExamAnswer> list) {
        int num = examTestMapper.insertAndGetId(examTest);
        for(ExamAnswer examAnswer:list){
            examAnswer.setTestid(examTest.getId());
            examAnswerMapper.insertSelective(examAnswer);
        }

        return num;
    }

    @Override
    public int editExamTestAndExamAnswer(ExamTest examTest, List<ExamAnswer> list,String deleteExamTestIds) {
        int num=examTestMapper.updateByPrimaryKeySelective(examTest);
        for(ExamAnswer examAnswer:list){
            examAnswer.setTestid(examTest.getId());
            if(examAnswer.getId()==null){
                examAnswerMapper.insertSelective(examAnswer);
            }else{
                examAnswerMapper.updateByPrimaryKeySelective(examAnswer);
            }

        }
        examAnswerService.deleteExamByIds(deleteExamTestIds);

        return num;
    }

    @Override
    public int deleteExamTestByPrimaryKey(Integer id) {
        int num = examTestMapper.deleteByPrimaryKey(id);
        return num;
    }

    @Override
    public int deleteExamByIds(String ids) {
        int num = 0;
        if (ids != null&&!"".equals(ids)) {
            String roids[] = ids.split(",");
            for (String roid : roids) {
                num += examTestMapper.deleteByPrimaryKey(Integer.parseInt(roid));
                examAnswerService.deleteExamByExamTestIds(ids);
            }
        }
        return num;
    }

    @Override
    public ExamTest getExamTestByPrimaryKey(Integer id) {
        ExamTest exam = examTestMapper.selectByPrimaryKey(id);
        return exam;
    }

    @Override
    public int updateExamTestByPrimaryKey(ExamTest ExamTest) {
        int num = examTestMapper.updateByPrimaryKeySelective(ExamTest);
        return num;
    }

    @Override
    public Page getExamTestTestList(ExamTest examTest, int pageSize, int pageNumber) {
        ExamTestExample examTestExample = new ExamTestExample();

        //获取总的记录数量
        int count = examTestMapper.countByExample(examTestExample);
        //获取指定的页码数据
        examTest.setBeginNum((pageNumber - 1) * pageSize);
        examTest.setEndNum(pageSize);
        List<ExamTest> list = examTestMapper.getListPage(examTest);
        //组合返回page
        Page page = new Page(pageNumber, count, pageSize, list);

        return page;
    }

    @Override
    public List<ExamTest> getByExamId(Integer id) {
        if(id != null && id > 0)
        {
            ExamTestExample examTestExample = new ExamTestExample();
            ExamTestExample.Criteria criteria = examTestExample.createCriteria();
            criteria.andExamidEqualTo(id);
            List<ExamTest> examTests = this.examTestMapper.selectByExample(examTestExample);

            //设置答案
            if(examTests != null && examTests.size() > 0)
            {
                for(ExamTest test:examTests)
                {
                    List<ExamAnswer> answerList = this.examAnswerService.getByTestId(test.getId());
                    if(answerList == null)
                    {
                        answerList = new ArrayList<ExamAnswer>();
                    }
                    test.setAnswerList(answerList);
                }
            }
            return examTests;
        }
        return null;
    }
}
